﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using Lists;
using GeneralLibs;
using System.Linq;
using System.Windows.Forms;

namespace Vouchers
{
    public class ThongSoMacDinhHoaDonBanle
    {

        #region TaiKhoanNoTienThuVe property
        private TaiKhoan _TaiKhoanNoTienThuVe;
        public TaiKhoan TaiKhoanNoTienThuVe
        {
            get
            {
                return _TaiKhoanNoTienThuVe;
            }
            set
            {
                _TaiKhoanNoTienThuVe = value;
            }
        }
        #endregion

        #region TaiKhoanCoDoanhThu property
        private TaiKhoan _TaiKhoanCoDoanhThu;
        public TaiKhoan TaiKhoanCoDoanhThu
        {
            get
            {
                return _TaiKhoanCoDoanhThu;
            }
            set
            {
                _TaiKhoanCoDoanhThu = value;
            }
        }
        #endregion

        #region TaiKhoanCoVat property
        private TaiKhoan _TaiKhoanCoVat;
        public TaiKhoan TaiKhoanCoVat
        {
            get
            {
                return _TaiKhoanCoVat;
            }
            set
            {
                _TaiKhoanCoVat = value;
            }
        }
        #endregion

        #region TaiKhoanNoGiaVon property
        private TaiKhoan _TaiKhoanNoGiaVon;
        public TaiKhoan TaiKhoanNoGiaVon
        {
            get
            {
                return _TaiKhoanNoGiaVon;
            }
            set
            {
                _TaiKhoanNoGiaVon = value;
            }
        }
        #endregion

        #region TaiKhoanCoXuatKho property
        private TaiKhoan _TaiKhoanCoXuatKho;
        public TaiKhoan TaiKhoanCoXuatKho
        {
            get
            {
                return _TaiKhoanCoXuatKho;
            }
            set
            {
                _TaiKhoanCoXuatKho = value;
            }
        }
        #endregion

        #region ThueSuatMacDinh property
        private string _ThueSuatMacDinh;
        public string ThueSuatMacDinh
        {
            get
            {
                return _ThueSuatMacDinh;
            }
            set
            {
                _ThueSuatMacDinh = value;
            }
        }
        #endregion
    }
    public partial class ChungTu
    {

        private void GhiSoButToanHoaDonBanLe(SqlCommand cmd)
        {
            //But toan thanh toan
            ButToan.InsertToDB(cmd, Ngay, ChungTuID, Guid.Empty, this.DoiTuongNghiepVu1ID,
                                    this.TaiKhoan1ID, this.TaiKhoan4ID, this.TienTeID, this.TyGiaThucTe, this.Value6,
                                    "Giá trị hóa đơn bán lẻ " + this.So);
            //but toan vat
            ButToan.InsertToDB(cmd, Ngay, ChungTuID, Guid.Empty, this.DoiTuongNghiepVu1ID,
                                    this.TaiKhoan1ID, this.TaiKhoan2ID, this.TienTeID, this.TyGiaThucTe, this.Value5,
                                    "VAT hóa đơn" + this.So, this.TenNguoiThucHien, DanhMucDoiTuongNghiepVu.Instance.GetByID(this.DoiTuongNghiepVu1ID).MaSoThue.ToString(), this.Text1.ToString(), this.So, this.Ngay,
                                    Guid.Empty, 0, this.ThueSuat1ID);

            foreach (ChungTu phieuXuatKho in _DanhSachChungTuPhuThuoc)
            {
                //But toan xuat kho
                ButToan.InsertToDB(cmd, Ngay, ChungTuID, Guid.Empty, this.DoiTuongNghiepVu1ID,
                                        phieuXuatKho.TaiKhoan1ID, phieuXuatKho.TaiKhoan2ID, this.TienTeID, this.TyGiaThucTe, phieuXuatKho.SoTienNT,
                                        this.DienGiai);
            }
        }

        public bool PrintChungTuHoaDonBanLe(bool quaCrystalReport)
        {
            ReportTemplates.BanLe.HoaDonBanLe.DsHoaDonBanLe ds = new ReportTemplates.BanLe.HoaDonBanLe.DsHoaDonBanLe();
            try
            {
                SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
                SqlCommand cmd = con.CreateCommand();
                SqlDataAdapter da = null; ;

                cmd.CommandText = "Select_ThongTinDoanhNghiep2";
                cmd.CommandType = CommandType.StoredProcedure;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "ThongTinDoanhNghiep");
                DataRow firstRowOfThongTinDoanhNghiep = ds.ThongTinDoanhNghiep.Rows[0];

                Store store = DanhMucStore.Instance.GetByStoreID(Config.StoreID);

                GeneralLibs.LocalSettings.HeThong.TenCuaHang = firstRowOfThongTinDoanhNghiep["TenDoanhNghiep"].ToString();
                GeneralLibs.LocalSettings.HeThong.DiaChi = firstRowOfThongTinDoanhNghiep["DiaChi"].ToString();
                GeneralLibs.LocalSettings.HeThong.DienThoai = firstRowOfThongTinDoanhNghiep["DienThoai"].ToString();

                cmd.CommandText = "Select_Master_HoaDonBanLe";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da.Fill(ds.Master_HoaDonBanLe);

                bool thanhToanQuaThe = false;
                if (ds.Master_HoaDonBanLe.Rows[0]["TTQuaThe"] is bool)
                    thanhToanQuaThe = (bool)ds.Master_HoaDonBanLe.Rows[0]["TTQuaThe"];
                if (thanhToanQuaThe)
                    ds.Master_HoaDonBanLe.Rows[0]["HinhThucThanhToan"] = "Qua thẻ";
                else
                    ds.Master_HoaDonBanLe.Rows[0]["HinhThucThanhToan"] = "Tiền mặt";

                decimal tongSoTien = (decimal)ds.Master_HoaDonBanLe.Rows[0]["TongSoTien"];
                ds.Master_HoaDonBanLe.Rows[0]["SoTienBangChu"] = GeneralLibs.CommonLib.DocTien((int)tongSoTien, "đồng.");

                cmd.CommandText = string.Format("SELECT B.Ma MaHangHoa, B.Ten TenHang, B.TenNganGon, A.Value1 SoLuong, A.Value12 GiaBia, A.Value13 ThanhTienGiaBia,  A.Value8 ChietKhau, A.Value7 ThanhTien \n" +
                                                "FROM KhoanMucChungTu A JOIN VatTuHangHoa B ON A.VatTuHangHoa1ID = B.ID \n" +
                                                "WHERE A.ChungTuLienQuanID = '{0}' \n" +
                                                "ORDER BY A.ThuTuKhoanMucTrongChungTu ASC", 
                                                ChungTuID);

                cmd.CommandType = CommandType.Text;
                da.Fill(ds.Detail_HoaDonBanLe);

                if (!store.Ten.ToUpper().Contains("IBOOK"))   //Cắt tên nếu muốn, ibook k cắt
                {
                    if (DanhMucStore.Instance.CurrentStore().Ten.ToUpper().Contains("THENAME"))
                    {
                        //The name để 
                        int doDai = 40;
                        foreach (DataRow row in ds.Detail_HoaDonBanLe.Rows)
                        {
                            if (((string)row["TenHang"]).Length > doDai)
                                row["TenHang"] = ((string)row["TenHang"]).Substring(0, doDai) + "..";
                        }
                    }
                    else
                    {
                        foreach (DataRow row in ds.Detail_HoaDonBanLe.Rows)
                        {
                            if (((string)row["TenHang"]).Length > 22)
                                row["TenHang"] = ((string)row["TenHang"]).Substring(0, 22) + "..";
                        }
                    }
                }
                else  //Nếu là ibook, ưu tiên dùng tên ngắn gọn trước
                {
                    foreach (DataRow row in ds.Detail_HoaDonBanLe.Rows)
                    {
                        if (row["TenNganGon"] is string && (string)row["TenNganGon"] != "")
                        {
                            row["TenHang"] = (string)row["TenNganGon"];
                        }
                    }
                }

                cmd.CommandText = "Select_UserCreateChungTu";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "User");

                con.Close();
                CrystalDecisions.CrystalReports.Engine.ReportClass report = null;

                if (store.Ten.ToUpper().Contains("XXX"))    //In A4 nếu nơi nào muốn. Hiện chưa dùng cho nơi nào
                {
                    report = new ReportTemplates.BanLe.RptPrintHoaDonA4();
                    report.SetDataSource(ds);
                    if (!quaCrystalReport)
                    {
                        report.PrintToPrinter(1, false, 0, 0);
                    }
                    else
                    {
                        ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(report, "In hóa đơn bán lẻ");
                        frm.Show();
                    }
                }
                else
                {
                    if (store.Ten.ToUpper().Contains("IBOOK"))
                        report = new ReportTemplates.BanLe.HoaDonBanLeNinhBinh.RptHoaDonBanLeIBOOK();
                    else if (store.Ten.ToUpper().Contains("PHSPHUTHO"))
                        report = new ReportTemplates.BanLe.HoaDonBanLeNinhBinh.RptHoaDonBanLeNinhBinhKhongCotCK();
                    else if (DanhMucStore.Instance.CurrentStore().Ten.ToUpper().Contains("THENAME"))
                        report = new ReportTemplates.BanLe.RptHoaDonBanLeTheName();
                    else
                        report = new ReportTemplates.BanLe.HoaDonBanLeNinhBinh.RptHoaDonBanLeNinhBinh();

                    report.SetDataSource(ds);

                    if (!quaCrystalReport)
                    {
                        report.PrintToPrinter(1, false, 0, 0);
                    }
                    else
                    {
                        ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(report, "In hóa đơn bán");
                        frm.Show();
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi khi in: \n" + ex.Message);
                return false;
            }
        }

        public bool PrintChungTuHoaDonBanLeForeignLanguage(bool quaCrystalReport)
        {
            ReportTemplates.BanLe.HoaDonBanLe.DsHoaDonBanLe ds = new ReportTemplates.BanLe.HoaDonBanLe.DsHoaDonBanLe();
            try
            {
                SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
                SqlCommand cmd = con.CreateCommand();
                SqlDataAdapter da = null; ;

                cmd.CommandText = "Select_ThongTinDoanhNghiep2";
                cmd.CommandType = CommandType.StoredProcedure;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "ThongTinDoanhNghiep");
                DataRow firstRowOfThongTinDoanhNghiep = ds.ThongTinDoanhNghiep.Rows[0];

                Store store = DanhMucStore.Instance.GetByStoreID(Config.StoreID);

                GeneralLibs.LocalSettings.HeThong.TenCuaHang = firstRowOfThongTinDoanhNghiep["TenDoanhNghiep"].ToString();
                GeneralLibs.LocalSettings.HeThong.DiaChi = firstRowOfThongTinDoanhNghiep["DiaChi"].ToString();
                GeneralLibs.LocalSettings.HeThong.DienThoai = firstRowOfThongTinDoanhNghiep["DienThoai"].ToString();

                cmd.CommandText = "Select_Master_HoaDonBanLe";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da.Fill(ds.Master_HoaDonBanLe);

                bool thanhToanQuaThe = false;
                if (ds.Master_HoaDonBanLe.Rows[0]["TTQuaThe"] is bool)
                    thanhToanQuaThe = (bool)ds.Master_HoaDonBanLe.Rows[0]["TTQuaThe"];
                if (thanhToanQuaThe)
                    ds.Master_HoaDonBanLe.Rows[0]["HinhThucThanhToan"] = "Qua thẻ";
                else
                    ds.Master_HoaDonBanLe.Rows[0]["HinhThucThanhToan"] = "Tiền mặt";

                decimal tongSoTien = (decimal)ds.Master_HoaDonBanLe.Rows[0]["TongSoTien"];
                ds.Master_HoaDonBanLe.Rows[0]["SoTienBangChu"] = GeneralLibs.CommonLib.DocTien((int)tongSoTien, "đồng.");

                cmd.CommandText = "Select_Detail_HoaDonBanLe";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da.Fill(ds.Detail_HoaDonBanLe);

                if (!store.Ten.ToUpper().Contains("IBOOK"))   //Cắt tên nếu muốn, ibook k cắt
                {
                    foreach (DataRow row in ds.Detail_HoaDonBanLe.Rows)
                    {
                        if (((string)row["TenHang"]).Length > 22)
                            row["TenHang"] = ((string)row["TenHang"]).Substring(0, 22) + "..";
                    }
                }
                else  //Nếu là ibook, ưu tiên dùng tên ngắn gọn trước
                {
                    foreach (DataRow row in ds.Detail_HoaDonBanLe.Rows)
                    {
                        if (row["TenNganGon"] is string && (string)row["TenNganGon"] != "")
                        {
                            row["TenHang"] = (string)row["TenNganGon"];
                        }
                    }
                }

                cmd.CommandText = "Select_UserCreateChungTu";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "User");

                con.Close();
                CrystalDecisions.CrystalReports.Engine.ReportClass report = null;

                report = new ReportTemplates.BanLe.RptHoaDonBanLe_Foreign();

                report.SetDataSource(ds);

                if (!quaCrystalReport)
                {
                    report.PrintToPrinter(1, false, 0, 0);
                }
                else
                {
                    ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(report, "In hóa đơn bán lẻ tiếng Anh");
                    frm.Show();
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi khi in: \n" + ex.Message);
                return false;
            }
        }

        public bool PrintChungTuHoaDonBanLeA4(bool quaCrystalReport)
        {
            ReportTemplates.BanLe.HoaDonBanLe.DsHoaDonBanLe ds = new ReportTemplates.BanLe.HoaDonBanLe.DsHoaDonBanLe();
            try
            {
                SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
                SqlCommand cmd = con.CreateCommand();
                SqlDataAdapter da = null; ;

                cmd.CommandText = "Select_ThongTinDoanhNghiep2";
                cmd.CommandType = CommandType.StoredProcedure;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "ThongTinDoanhNghiep");
                DataRow firstRowOfThongTinDoanhNghiep = ds.ThongTinDoanhNghiep.Rows[0];

                Store store = DanhMucStore.Instance.GetByStoreID(Config.StoreID);

                GeneralLibs.LocalSettings.HeThong.TenCuaHang = firstRowOfThongTinDoanhNghiep["TenDoanhNghiep"].ToString();
                GeneralLibs.LocalSettings.HeThong.DiaChi = firstRowOfThongTinDoanhNghiep["DiaChi"].ToString();
                GeneralLibs.LocalSettings.HeThong.DienThoai = firstRowOfThongTinDoanhNghiep["DienThoai"].ToString();

                cmd.CommandText = "Select_Master_HoaDonBanLe";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da.Fill(ds.Master_HoaDonBanLe);

                decimal tongSoTien = (decimal)ds.Master_HoaDonBanLe.Rows[0]["TongSoTien"];
                ds.Master_HoaDonBanLe.Rows[0]["SoTienBangChu"] = string.Format("({0})", GeneralLibs.CommonLib.DocTien((int)tongSoTien, "đồng."));

                cmd.CommandText = "Select_Detail_HoaDonBanLe";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da.Fill(ds.Detail_HoaDonBanLe);

                cmd.CommandText = "Select_UserCreateChungTu";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "User");

                con.Close();

                ReportTemplates.BanLe.RptPrintHoaDonA4 report = new ReportTemplates.BanLe.RptPrintHoaDonA4();
                report.SetDataSource(ds);

                if (!quaCrystalReport)
                {
                    report.PrintToPrinter(1, false, 0, 0);
                }
                else
                {
                    ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(report, "In hóa đơn bán");
                    frm.Show();
                }

                ////Dùng khi không in được tự động thì in qua report
                //if (DanhMucStore.Instance.GetByStoreID(Config.StoreID).Ten.Trim().ToUpper().Contains("BachKhoa"))
                //{
                //    ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(report, "In hóa đơn bán");
                //    frm.Show();
                //}
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi khi in: \n" + ex.Message);
                return false;
            }
        }

        public bool PrintVoucherHuyHoangBookCoffee(bool quaCrystalReport)
        {

            ReportTemplates.BanLe.HoaDonBanLe.DsHoaDonBanLe ds = new ReportTemplates.BanLe.HoaDonBanLe.DsHoaDonBanLe();
            try
            {
                SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
                SqlCommand cmd = con.CreateCommand();
                SqlDataAdapter da = null; ;

                cmd.CommandText = "Select_ThongTinDoanhNghiep2";
                cmd.CommandType = CommandType.StoredProcedure;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "ThongTinDoanhNghiep");
                DataRow firstRowOfThongTinDoanhNghiep = ds.ThongTinDoanhNghiep.Rows[0];

                Store store = DanhMucStore.Instance.GetByStoreID(Config.StoreID);

                GeneralLibs.LocalSettings.HeThong.TenCuaHang = firstRowOfThongTinDoanhNghiep["TenDoanhNghiep"].ToString();
                GeneralLibs.LocalSettings.HeThong.DiaChi = firstRowOfThongTinDoanhNghiep["DiaChi"].ToString();
                GeneralLibs.LocalSettings.HeThong.DienThoai = firstRowOfThongTinDoanhNghiep["DienThoai"].ToString();

                cmd.CommandText = "Select_Master_HoaDonBanLe";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da.Fill(ds.Master_HoaDonBanLe);

                decimal tongSoTien = (decimal)ds.Master_HoaDonBanLe.Rows[0]["TongSoTien"];
                //ds.Master_HoaDonBanLe.Rows[0]["SoTienBangChu"] = string.Format("({0})", GeneralLibs.CommonLib.DocTien((int)tongSoTien));

                cmd.CommandText = "Select_Detail_HoaDonBanLe";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da.Fill(ds.Detail_HoaDonBanLe);
                foreach (DataRow row in ds.Detail_HoaDonBanLe.Rows)
                {
                    if (((string)row["TenHang"]).Length > 22)
                        row["TenHang"] = ((string)row["TenHang"]).Substring(0, 22) + "..";
                }

                cmd.CommandText = "Select_UserCreateChungTu";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "User");

                con.Close();
                ds.NgayHetHanVoucher.Rows.Add(Ngay.Date.AddDays(30));

                ReportTemplates.BanLe.HoaDonBanLeNinhBinh.VoucherBookCoffee report = new ReportTemplates.BanLe.HoaDonBanLeNinhBinh.VoucherBookCoffee();
                report.SetDataSource(ds);

                if (!quaCrystalReport)
                {
                    report.PrintToPrinter(1, false, 0, 0);
                }
                else
                {
                    ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(report, "In hóa đơn bán");
                    frm.Show();
                }

                ////Dùng khi không in được tự động thì in qua report
                //if (DanhMucStore.Instance.GetByStoreID(Config.StoreID).Ten.Trim().ToUpper().Contains("BachKhoa"))
                //{
                //    ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(report, "In hóa đơn bán");
                //    frm.Show();
                //}
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi khi in: \n" + ex.Message);
                return false;
            }
        }

        public bool PrintChungTuHoaDonBanLe_VietBook2013(bool quaCrystalReport)
        {
            ReportTemplates.BanLe.HoaDonBanLe.DsHoaDonBanLe ds = new ReportTemplates.BanLe.HoaDonBanLe.DsHoaDonBanLe();
            try
            {
                SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
                SqlCommand cmd = con.CreateCommand();
                SqlDataAdapter da = null; ;

                cmd.CommandText = "Select_ThongTinDoanhNghiep2";
                cmd.CommandType = CommandType.StoredProcedure;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "ThongTinDoanhNghiep");
                DataRow firstRowOfThongTinDoanhNghiep = ds.ThongTinDoanhNghiep.Rows[0];

                firstRowOfThongTinDoanhNghiep["TenDoanhNghiep"] = GeneralLibs.LocalSettings.HeThong.TenCuaHang;
                firstRowOfThongTinDoanhNghiep["DiaChi"] = GeneralLibs.LocalSettings.HeThong.DiaChi;
                firstRowOfThongTinDoanhNghiep["DienThoai"] = GeneralLibs.LocalSettings.HeThong.DienThoai;

                cmd.CommandText = "Select_Master_HoaDonBanLe";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da.Fill(ds.Master_HoaDonBanLe);

                decimal tongSoTien = (decimal)ds.Master_HoaDonBanLe.Rows[0]["TongSoTien"];
                //ds.Master_HoaDonBanLe.Rows[0]["SoTienBangChu"] = string.Format("({0})", GeneralLibs.CommonLib.DocTien((int)tongSoTien));

                cmd.CommandText = "Select_Detail_HoaDonBanLe";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da.Fill(ds.Detail_HoaDonBanLe);
                foreach (DataRow row in ds.Detail_HoaDonBanLe.Rows)
                {
                    if (((string)row["TenHang"]).Length > 22)
                        row["TenHang"] = ((string)row["TenHang"]).Substring(0, 22) + "..";
                }

                cmd.CommandText = "Select_UserCreateChungTu";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@ChungTuID", ChungTuID);
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "User");

                con.Close();

                ReportTemplates.BanLe.RptHoaDonBanLe_VietBook2013 report = new ReportTemplates.BanLe.RptHoaDonBanLe_VietBook2013();
                report.SetDataSource(ds);

                if (!quaCrystalReport)
                {
                    report.PrintToPrinter(1, false, 0, 0);
                }
                else
                {
                    ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(report, "In hóa đơn bán");
                    frm.Show();
                }

                ////Dùng khi không in được tự động thì in qua report
                //if (DanhMucStore.Instance.GetByStoreID(Config.StoreID).Ten.Trim().ToUpper().Contains("BachKhoa"))
                //{
                //    ReportTemplates.FrmPrintReport frm = new ReportTemplates.FrmPrintReport(report, "In hóa đơn bán");
                //    frm.Show();
                //}
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi khi in: \n" + ex.Message);
                return false;
            }
        }
    }
}
